﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGServer.Contracts.Messages
{
    static class MessageROFM
    {
        /// <summary>
        /// 通知地图上的其他人，该玩家离开了此地图
        /// </summary>
        /// <param name="player"></param>
        public static void Send(RPGPlayer player)
        {
            RPGMapPlayerCollection map = CacheData.GetInstance().GetMap(player.CurMapId);
            if (map == null)
            {
                return;
            }
            byte[] data = new byte[255];
            int offset = 0;
            // scenario
            IntUtil.ByteToBuffer(RPGNET_SCENARIO.SER_MSG, data, offset, out offset);
            // command
            IntUtil.ByteToBuffer(RPGNET_MESSAGE.REMOVE_OBJECT, data, offset, out offset);
            // player id
            IntUtil.UIntToBuffer(player.Id, data, offset, out offset);
            // object id
            IntUtil.UIntToBuffer(player.RoleId, data, offset, out offset);
            // object name
            IntUtil.StringToBuffer(String.Format("object{0}", player.RoleId), data, offset, 16, out offset);
            // object model id
            IntUtil.UIntToBuffer(player.RoleModelId, data, offset, out offset);
            // object level
            IntUtil.ByteToBuffer(0x01, data, offset, out offset);
            // map id
            IntUtil.UShortToBuffer(player.CurMapId, data, offset, out offset);
            // pos row
            IntUtil.UShortToBuffer(player.CurPosRow, data, offset, out offset);
            // pos col
            IntUtil.UShortToBuffer(player.CurPosCol, data, offset, out offset);
            // object direction
            IntUtil.ByteToBuffer(0x00, data, offset, out offset);

            for (int index = 0; index < map.PlayerList.Count; index++)
            {
                RPGPlayer oPlayer = map.PlayerList[index];
                if (oPlayer != null && oPlayer.Id != player.Id)
                {
                    oPlayer.Client.SendData(data, offset);
                }
            }
        }
    }
}
